﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using Continental.Code.BL;
using Continental.Code.Entity;

namespace Continental.Admin
{
    public partial class ManagerTuDiem : System.Web.UI.Page
    {
        public Int32 amountOfTuDiem = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadNhomTuDiemLevel1();
                LoadQuanHuyen();

                this.ddlDuongPho.Enabled = false;
                LoadTuDiem("", -1, -1, -1, true);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManagerAccountTuDiemAdd.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void LoadNhomTuDiemLevel1()
        {
            this.ddlNhomTuDiemLevel1.DataSource = TuDiemProvider.GetNhomTuDiemByParentID(0);
            this.ddlNhomTuDiemLevel1.DataBind();
        }

        private void LoadQuanHuyen()
        {
            this.ddlQuanHuyen.DataSource = ExtensionProvider.GetQuanHuyenList();
            this.ddlQuanHuyen.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            String titleOrName = this.txtNameForSearch.Text.Trim();
            Int32 nhomID = Int32.Parse(this.ddlNhomTuDiemLevel1.SelectedValue);
            Int32 quanHuyenID = Int32.Parse(this.ddlQuanHuyen.SelectedValue);
            Int32 duongPhoID = -1;
            try
            {
                duongPhoID = Int32.Parse(this.ddlDuongPho.SelectedValue);
            }
            catch
            {
                duongPhoID = -1;
            }
            Boolean deletedFlag = this.ckDeletedFlag.Checked;

            LoadTuDiem(titleOrName, nhomID, quanHuyenID, duongPhoID, deletedFlag);
        }

        private void LoadTuDiem(String titleOrName, Int32 nhomID, Int32 quanHuyenID, Int32 duongPhoID, Boolean deletedFlag)
        {
            DataSet dsResult = TuDiemProvider.GetTuDiemListByFillter(titleOrName, nhomID, quanHuyenID, duongPhoID, deletedFlag);
            amountOfTuDiem = dsResult.Tables[0].Rows.Count;
            this.lvTuDiem.DataSource = dsResult;
            this.lvTuDiem.DataBind();
        }

        protected void dpTuDiem_PreRender(object sender, EventArgs e)
        {
            String titleOrName = this.txtNameForSearch.Text.Trim();
            Int32 nhomID = Int32.Parse(this.ddlNhomTuDiemLevel1.SelectedValue);
            Int32 quanHuyenID = Int32.Parse(this.ddlQuanHuyen.SelectedValue);
            Int32 duongPhoID = -1;
            try
            {
                duongPhoID = Int32.Parse(this.ddlDuongPho.SelectedValue);
            }
            catch
            {
                duongPhoID = -1;
            }
            Boolean deletedFlag = this.ckDeletedFlag.Checked;

            LoadTuDiem(titleOrName, nhomID, quanHuyenID, duongPhoID, deletedFlag);
        }

        protected void ddlNhomTuDiemLevel1_DataBound(object sender, EventArgs e)
        {
            ListItem theItem = new ListItem("Tất cả", "-1");
            ddlNhomTuDiemLevel1.Items.Insert(0, theItem);
        }

        protected void ddlQuanHuyen_DataBound(object sender, EventArgs e)
        {
            ListItem theItem = new ListItem("Tất cả", "-1");
            ddlQuanHuyen.Items.Insert(0, theItem);
        }

        protected void ddlDuongPho_DataBound(object sender, EventArgs e)
        {
            ListItem theItem = new ListItem("Tất cả", "-1");
            ddlDuongPho.Items.Insert(0, theItem);
        }

        protected void ddlQuanHuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 quanHuyenID = Int32.Parse(this.ddlQuanHuyen.SelectedValue);
            if (quanHuyenID != -1)
            {
                this.ddlDuongPho.DataSource = ExtensionProvider.GetDuongPhoList(quanHuyenID);
                this.ddlDuongPho.DataBind();
                if (ddlDuongPho.Items.Count == 1)
                {
                    this.ddlDuongPho.Enabled = false;
                }
                else
                {
                    this.ddlDuongPho.Enabled = true;
                }
            }
            else
            {
                this.ddlDuongPho.DataSource = null;
            }
        }
    }
}
